iOS Recipes (for Dee Robinson) by Matt Drance & Paul Warren

iOS Recipes (for Dee Robinson) by Matt Drance & Paul Warren

Author:Matt Drance & Paul Warren
Language: eng
Format: epub
Tags: Pragmatic Bookshelf
ISBN: 978-1-934356-74-6
Publisher: The Pragmatic Bookshelf, LLC (291020)


* * *

Figure 23. Table view border shadows

Once again we’re inserting subviews to represent the shadows. Why wouldn’t we just set the shadows as part of the standard table header and footer? Well, first of all, doing this would affect the table’s content size. If we set the bottom shadow as the table footer, for example, the table would include room at the bottom for the content shadow so that it was always visible. That isn’t what we want: we want the shadows to have no effect on the actual content size and to be visible only when the respective top or bottom ends of the content are exposed. Second, we want this class to support custom headers and footers without affecting their layout or behavior. Using subviews that are independent of the other content gives us the greatest flexibility and safety.

We start with a common initializer that is called whether the table is created in code or in Interface Builder. This ‑commonInit method installs the four shadow views and performs some additional initialization.

ShadowedTables/Classes/PRPShadowedTableView.m

- (void)commonInit {

[self installShadows];

}

The previously shown screenshot has a table view that doesn’t fill the screen, making the bottom two shadows always visible. In the screenshot, the table is also pulled down past the top of its content, revealing the two top shadows as well. This shadow placement creates an appearance that resembles the Clock app.

The ‑installShadows method invoked by ‑commonInit initializes the four shadow views to use one of two shadow images. Both images are 1 pixel wide and safely stretchable to the left or right without any modifications. Each shadow view is then set up by the ‑installShadow: method for use in our table view. This step makes the shadows adaptable to any screen—iPhone, iPad, or whatever else comes along in the future.

ShadowedTables/Classes/PRPShadowedTableView.m

UIImage *upShadow = [UIImage imageNamed:@"shadowUp.png"];

UIImage *downShadow = [UIImage imageNamed:@"shadowDown.png"];

ShadowedTables/Classes/PRPShadowedTableView.m

- (void)installShadow:(UIImageView *)shadowView {

shadowView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

CGRect contentFrame = shadowView.frame;

contentFrame.size.width = self.frame.size.width;

shadowView.frame = contentFrame;

[self repositionShadow:shadowView];

}

Now that the shadow subviews are installed, we can work on positioning them. The easiest shadow to manage is the shadow above the top of the table’s content. This shadow should freely move with the rest of the content but stay above the top while not actually affecting the content size. We accomplish this by setting a negative Y origin on the shadow. Since this never changes, we need to do it only once, so it’s done early on from ‑installShadows.

ShadowedTables/Classes/PRPShadowedTableView.m

if (contentTopShadow == nil) {

contentTopShadow = [[UIImageView alloc] initWithImage:upShadow];

[self installShadow:contentTopShadow];

CGRect topShadowFrame = contentTopShadow.frame;

topShadowFrame.origin.y = -topShadowFrame.size.height;

contentTopShadow.frame = topShadowFrame;

}

The fixed shadows at the top and bottom of the table view require some more work. When the user scrolls a scroll view or table view, all of the subviews move accordingly, unless we do something special in ‑layoutSubviews. Our ‑layoutSubviews implementation first passes the message on to super to preserve the default UITableView behavior and then sends ‑updateShadows to adjust the other three shadow views as needed.

ShadowedTables/Classes/PRPShadowedTableView.m

- (void)layoutSubviews {

[super layoutSubviews];

[self updateShadows];

}

First we update the fixed shadow at the top of the table. Because this subview



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.